home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / program / ixemlsrc.lha / ixemul / library / __write.c < prev    next >
C/C++ Source or Header  |  1995-12-23  |  3KB  |  104 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  __write.c,v 1.1.1.1 1994/04/04 04:30:14 amiga Exp
  20.  *
  21.  *  __write.c,v
  22.  * Revision 1.1.1.1  1994/04/04  04:30:14  amiga
  23.  * Initial CVS check in.
  24.  *
  25.  *  Revision 1.2  1992/08/09  20:40:39  amiga
  26.  *  add a cast to get rid of a warning
  27.  *
  28.  *  Revision 1.1  1992/05/14  19:55:40  mwild
  29.  *  Initial revision
  30.  *
  31.  */
  32.  
  33. #define KERNEL
  34. #include "ixemul.h"
  35. #include "kprintf.h"
  36.  
  37. int
  38. __write(struct file *f, char *buf, int len)
  39. {
  40.   int err=errno, res = 0;
  41.   int omask;
  42.  
  43.   if (HANDLER_NIL(f)) return len;
  44.   if (f->f_fh->fh_Port)    /* if interactive */
  45.     return __sync_write(f, buf, len);
  46.  
  47.   omask = syscall (SYS_sigsetmask, ~0);
  48.   __get_file (f);
  49.   /* get results of last call */
  50.   __wait_packet(&f->f_sp);
  51.   res = LastResult(f);
  52.   if (res == -1)
  53.     err = __ioerr_to_errno(LastError(f));
  54.   else
  55.     {
  56.       /* we have no way of telling, whether the last written number
  57.        * of characters was correct or not, we just say, if it wasn't
  58.        * an error (res == -1), than it's ok to continue writing to this
  59.        * file. We have to return 'len' instead of 'res', because the 
  60.        * user normally expects to say "while write(fd,buf,len)==len"
  61.        * as a check whether there was an error with write() or not */
  62.  
  63.       res = len;
  64.  
  65.       if (len > 0)
  66.         {
  67.           /* right append-mode means, before each write do an explicit
  68.            * seek to eof */
  69.           if (f->f_flags & FAPPEND) 
  70.         {
  71.           SendPacket3(f,__rwport,ACTION_SEEK,f->f_fh->fh_Arg1,0,OFFSET_END);
  72.               __wait_packet(&f->f_sp);
  73.         }
  74.  
  75.       /* We need to save a copy of this buffer in our local buffer,
  76.        * because the caller is free to change buf after this function
  77.        * returns. */
  78.       if (len > f->f_async_len) 
  79.         {
  80.           KPRINTF (("asl=%ld,l=%ld,b=$%lx ",f->f_async_len, len,f->f_async_buf));
  81.           f->f_async_len = len*2;    /* don't waste too much memory */
  82.           if (! (f->f_async_buf = (char *)krealloc (f->f_async_buf, f->f_async_len)))
  83.             {
  84.           f->f_async_len = 0;
  85.           /* in that case resort to sync write */
  86.           __release_file (f);
  87.           syscall (SYS_sigsetmask, omask);
  88.           errno = err;
  89.           KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  90.           return __sync_write (f, buf, len);
  91.         }
  92.         }
  93.       bcopy (buf, f->f_async_buf, len);
  94.           SendPacket3(f,__rwport,ACTION_WRITE,f->f_fh->fh_Arg1,(long)f->f_async_buf,len);
  95.         }
  96.     }
  97.  
  98.   __release_file (f);
  99.   syscall (SYS_sigsetmask, omask);
  100.   errno = err;
  101.   KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  102.   return res;
  103. }
  104.